var Bind = {
    create: function (e, t, i, a, r, o, s) {
        var l = {
            inc: 1,
            name: e,
            exp: a,
            lp: r,
            scopes: {},
            targets: {},
            children: null,
            lu: s,
            __scopeBinded: []
        };
        for (var d = 0; d < o.length; d++) {
            var A = o[d];
            null === A[0] ? n(t, A, null, l) : n(i[A[0]], A, null, l);
        }
        return l;
    },
    proxyTopScope: function (e) {
        var t = {
            inc: 1,
            linked: e,
            scopes: {},
            targets: {},
            children: null,
            __scopeBinded: []
        };
        n(e, [null], null, t);
        return t;
    },
    proxySubScopes: function (e) {
        var t = {};
        for (var i in e)
            t[i] = {
                inc: 1,
                linked: e[i],
                scopes: {},
                targets: {},
                children: null,
                __scopeBinded: []
            }, n(e[i], [null], null, t[i]);
        return t;
    }
};
var n = Bind.addBinding = function (e, t, i, n) {
    var a = e;
    for (var r = 1; r < t.length; r++) {
        var o = t[r];
        a.children || (a.children = Object.create(null));
        var s = a.children;
        s[o] || (s[o] = {
            scopes: {},
            targets: {},
            children: null
        });
        a = s[o];
    }
    var l = e.inc++;
    if (i) {
        a.targets[l] = [
            i,
            n
        ];
    } else {
        a.scopes[l] = n;
        n.__scopeBinded.push([
            a,
            l
        ]);
    }
    return l;
};
Bind.addBindings = function (e, t, i, a, r) {
    for (var o = 0; o < i.length; o++) {
        var s = i[o];
        s[0] ? n(t[s[0]], s, a, r) : n(e, s, a, r);
    }
};
Bind.updateLvaluePath = function (e, t) {
    e.lp = t;
};
Bind.removeBindingsForScope = function (e) {
    var t = e.__scopeBinded;
    for (var i = 0; i < t.length; i++) {
        var n = t[i];
        delete n[0].scopes[n[1]];
    }
};
var a = function (e, t, i, n, a) {
    if (e.linked)
        r(e, t, i, n, a);
    else {
        var o = n[e.name];
        n[e.name] = e.exp(i, n, a);
        r(e, t, i, n, a);
        n[e.name] = o;
    }
};
var r = Bind.updateBinding = function (e, t, i, n, r) {
    var o = e;
    var s = 0;
    for (var l = 0; l < t.length; l++) {
        for (s in o.scopes)
            a(o.scopes[s], t, i, n, r);
        var d = t[l];
        if (!o.children)
            return;
        var A = o.children;
        if (!A[d])
            return;
        o = A[d];
    }
    var c = function (e) {
        for (s in e.targets) {
            var t = e.targets[s];
            var o = t[1];
            o.o(t[0], o.n, o.e(i, n, r));
        }
        for (s in e.scopes)
            a(e.scopes[s], [], i, n, r);
        for (s in e.children)
            c(e.children[s]);
    };
    c(o);
};
module.exports = Bind;
